package CCRMClient.GUI;

import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ReportManager;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextField;

import java.awt.SystemColor;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import java.awt.Component;
import java.io.IOException;

/**
 * This Class implements the reports UCs
 * @author Ori Ziv
 *
 */
public class ReportsScreen extends JPanel {
	private JLabel lblCcrmReports;
	private JButton btnCustomersReport;
	private JButton btnSalesReport;
	private JButton btnGoBack;
	private JTextField textField;
	private JLabel lblSaleId;

	/**
	 * Create the panel.
	 */
	public ReportsScreen() {

		initialize();
	}
	private void initialize() {
		setSize (469, 281);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblCcrmReports());
		add(getBtnCustomersReport());
		add(getBtnSalesReport());
		add(getBtnGoBack());
		add(getTextField());
		add(getLblSaleId());
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getBtnCustomersReport(), getTextField(), getBtnSalesReport(), getBtnGoBack()}));
	}
	JLabel getLblCcrmReports() {
		if (lblCcrmReports == null) {
			lblCcrmReports = new JLabel("CCRM Reports");
			lblCcrmReports.setHorizontalAlignment(SwingConstants.CENTER);
			lblCcrmReports.setForeground(Color.RED);
			lblCcrmReports.setFont(new Font("David", Font.BOLD, 26));
			lblCcrmReports.setBackground(Color.WHITE);
			lblCcrmReports.setBounds(0, 0, 461, 49);
		}
		return lblCcrmReports;
	}
	JButton getBtnCustomersReport() {
		if (btnCustomersReport == null) {
			btnCustomersReport = new JButton("Customers Report");
			btnCustomersReport.addActionListener(new BtnCustomersReportActionListener());
			btnCustomersReport.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCustomersReport.setBounds(10, 60, 168, 23);
		}
		return btnCustomersReport;
	}
	JButton getBtnSalesReport() {
		if (btnSalesReport == null) {
			btnSalesReport = new JButton("Sales Report");
			btnSalesReport.addActionListener(new BtnSalesReportActionListener());
			btnSalesReport.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSalesReport.setBounds(10, 130, 168, 23);
		}
		return btnSalesReport;
	}
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(299, 185, 124, 23);
		}
		return btnGoBack;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new EmployeePortal());
		}
	}
	/**
	 * This inner class implements event listener for pressing Customers Report button
	 * @author Ori Ziv
	 *
	 */
	private class BtnCustomersReportActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.CUSTOMERS_REPORT, ReportManager.customerReport());
			} catch (IOException ex) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			}
		}
	}
	
	/**
	 * This inner class implements event listener for pressing Sales Report button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSalesReportActionListener implements ActionListener{
		public void actionPerformed(ActionEvent e) {
			try{
				String saleID=textField.getText();
				if (saleID.isEmpty()) throw new NullPointerException();
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SALES_REPORT, ReportManager.salesReport(SERVER_COMMANDS.SALES_REPORT,saleID));
			} catch (IOException ex) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			} catch (NullPointerException e1) {
				JOptionPane.showMessageDialog(null, "Please type sale ID", "Sales Report", JOptionPane.INFORMATION_MESSAGE);
			}
			
		}
	}
	JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setColumns(10);
			textField.setBackground(SystemColor.menu);
			textField.setBounds(172, 99, 251, 20);
		}
		return textField;
	}
	JLabel getLblSaleId() {
		if (lblSaleId == null) {
			lblSaleId = new JLabel("Sale ID:");
			lblSaleId.setFont(new Font("David", Font.BOLD, 20));
			lblSaleId.setBounds(70, 94, 104, 29);
		}
		return lblSaleId;
	}
}
